insmod传递带空格的字符串 | 您所在的位置:网站首页 › insmod 参数 › insmod传递带空格的字符串 |
Makefile内容
KVERS = $(shell uname -r)
# Kernel modules
obj-m += book.o
# Specify flags for the module compilation.
#EXTRA_CFLAGS=-g -O0
build: kernel_modules
kernel_modules:
make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules
clean:
make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean
book.c内容--使用宋老师的例子
* a simple kernel module supporting parameters
*
* Copyright (C) 2014 Barry Song ([email protected])
*
* Licensed under GPLv2 or later.
*/
#include
#include
static char *book_name = "dissecting Linux Device Driver";
module_param(book_name, charp, S_IRUGO);
static int book_num = 4000;
module_param(book_num, int, S_IRUGO);
static int __init book_init(void)
{
printk(KERN_INFO "book name:%s\n", book_name);
printk(KERN_INFO "book num:%d\n", book_num);
return 0;
}
module_init(book_init);
static void __exit book_exit(void)
{
printk(KERN_INFO "book module exit\n ");
}
module_exit(book_exit);
MODULE_AUTHOR("Barry Song ");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("A simple Module for testing module params");
MODULE_VERSION("V1.0");
默认加载
sudo insmod book.ko 输出如下 [41709.146814] book name:dissecting Linux Device Driver [41709.146818] book num:4000 加载时传递参数sudo insmod book.ko book_name='GoodBook' book_num=5000 输出如下 [41933.147764] book name:GoodBook [41933.147769] book num:5000 加载时传递参数--字符串里包含空格(错误方式)sudo insmod book.ko book_name='Professional Linux Kernel Architecture' book_num=5000 或者 sudo insmod book.ko book_name="Professional Linux Kernel Architecture" book_num=5000 提示错误 insmod: ERROR: could not insert module book.ko: Unknown symbol in moduledmesg输出 [42208.316805] book: Unknown parameter `Linux'原因在于:shell将' '或" "吃掉了,剩下book_name=Professional Linux Kernel Architecture作为参数传递给内核,由内核函数parse_args进行解析[kernel/params.c],然后调用next_arg将其分割为param和val匹配对,以等号作为分割符号,next_arg函数如下([kernel/params.c]): /* You can use " around spaces, but can't escape". / / Hyphens and underscores equivalent in parameter names. */ static char *next_arg(char *args, char **param, char **val) { unsigned int i, equals = 0; int in_quote = 0, quoted = 0; char *next; if (*args == '"') {/* 出现双引号 */ args++; in_quote = 1; quoted = 1; } for (i = 0; args[i]; i++) { if (isspace(args[i]) && !in_quote) break; /* 无双引号配对后出现空格时跳出for循环 */ if (equals == 0) { if (args[i] == '=') equals = i;/* 出现等号后记录其位置 */ } if (args[i] == '"')/* 判断双引号" */ in_quote = !in_quote;/* 双引号必须配对使用 */ } *param = args; if (!equals) *val = NULL;/* 无等号出现时赋值为空, */ else { args[equals] = '\0'; *val = args + equals + 1;/* 指向配对字符串的开始位置*/ /* Don't include quotes in value. */ if (**val == '"') { (*val)++; if (args[i-1] == '"') args[i-1] = '\0'; } if (quoted && args[i-1] == '"') args[i-1] = '\0'; } if (args[i]) { args[i] = '\0'; next = args + i + 1; } else next = args + i; /* Chew up trailing spaces. */ return skip_spaces(next); }根据上面函数分析,需要将传递给kernel时包括双引号内的内容是可以包含空格的,因此insmod时将"Professional Linux Kernel Architecture"外再用单引号包裹一层传递给shell即:'"Professional Linux Kernel Architecture"',最后传递给kernel时变成"Professional Linux Kernel Architecture",kernel即可正常识别。 加载时传递参数--字符串里包含空格(正确方式)sudo insmod book.ko book_name='"Professional Linux Kernel Architecture"' book_num=5000 输出 [45955.423536] book name:Professional Linux Kernel Architecture [45955.423540] book num:5000 |
CopyRight 2018-2019 实验室设备网 版权所有 |